package com.example.recordingpen_datacollection.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.recordingpen_datacollection.Utils.VerificationCodeGenerator;
import com.example.recordingpen_datacollection.entity.Identification;
import com.example.recordingpen_datacollection.entity.Login;
import com.example.recordingpen_datacollection.entity.UserLogin;
import com.example.recordingpen_datacollection.entity.Vo.BusinesVo;
import com.example.recordingpen_datacollection.entity.VoiceLogin;
import com.example.recordingpen_datacollection.mapper.LoginMapper;
import com.example.recordingpen_datacollection.redisService.RedisService;
import com.example.recordingpen_datacollection.result.exceptionhandler.JWException;
import com.example.recordingpen_datacollection.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.swing.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xg
 * @since 2023-11-08
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements LoginService {

    @Autowired
    private SmsService smsService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private BusinessdataService businessdataService;
    @Autowired
    private UserLoginService userLoginService;


    @Autowired
    private LogQuotaService logQuotaService;
    @Autowired
    private LogEndtimeService logEndtimeService;
    @Autowired
    private DosageStatisticsService dosageStatisticsService;
    @Autowired
    private AiconsultService aiconsultService;
    @Autowired
    private VoiceLoginService voiceLoginService;

    @Override
    public boolean registerAcc(String phone) {
        String code = VerificationCodeGenerator.generateVerificationCode(6);

        boolean b = smsService.sendSms(phone, code);
        if(b){
            redisService.setTokenValue2(phone,301, code);
        }

        return b;
    }

    @Override
    @Transactional
    public Login registerMessage(String phone) {
        Login login = new Login();
        login.setAccount(phone);
        login.setPassword(phone);
        login.setPhone(phone);
        boolean save = this.save(login);
        if(save){
            BusinesVo businesVo = new BusinesVo();
            businesVo.setLoginId(login.getId());
            businesVo.setPhone(phone);
            businesVo.setDosage(100000L);
            businesVo.setChargeType(1);

            businessdataService.saveBusiness(businesVo,null);
        }
        if(save){
            return login;
        } else {
            return null;
        }

    }

    @Override
    @Transactional
    public Login registerMessage(String phone, Identification identification) {

        Login login = new Login();
        login.setAccount(phone);
        login.setPassword(phone);
        login.setPhone(phone);

        boolean save = this.save(login);
        if(save){
            UserLogin userLogin = new UserLogin();
            userLogin.setUid(identification.getUid());
            userLogin.setDevid(login.getId());
            boolean save1 = userLoginService.save(userLogin);
            if(!save1){
                throw new JWException(500,"添加异常");
            }

            BusinesVo businesVo = new BusinesVo();
            businesVo.setLoginId(login.getId());
            businesVo.setPhone(phone);
            businesVo.setDosage(100000L);
            businesVo.setChargeType(identification.getId());
            businessdataService.saveBusiness(businesVo,identification.getDefaultValue());
            VoiceLogin voiceLogin = new VoiceLogin();
            voiceLogin.setLogId(login.getId());
            voiceLogin.setVlSumdosage(identification.getVoiceDefault());
            boolean save2 = voiceLoginService.save(voiceLogin);
            if(!save2){
                throw new JWException(500,"添加异常");
            }
        }
        if(save){
            return login;
        } else {
            return null;
        }
    }

    @Override
    @Transactional
    public boolean delDevice(Integer devid) {
        QueryWrapper delWrapper = new QueryWrapper();
        delWrapper.eq("login_id",devid);
        boolean remove = logQuotaService.remove(delWrapper);
        boolean remove1 = logEndtimeService.remove(delWrapper);
        boolean remove2 = dosageStatisticsService.remove(delWrapper);
        boolean remove3 = businessdataService.remove(delWrapper);
        boolean remove4 = aiconsultService.remove(delWrapper);
        QueryWrapper delWrapper2 = new QueryWrapper();
        delWrapper2.eq("devid",devid);
        boolean remove5 = userLoginService.remove(delWrapper2);
        boolean b = this.removeById(devid);
        return b;
    }


}
